﻿#include <iostream>
#include <graphics.h>
#include <string>
#include <vector>
#include<cmath>
#include"bullet.h"
#include"picture.h"
#include"button.h"
#include"player.h"
#include"enemy.h"
#include"bullet.h"

using namespace std;
#pragma comment(lib,"MSIMG32.LIB")

void drawscore(int score)
{
	static TCHAR text[64];
	_stprintf_s(text, _T("现在玩家的分：%d"), score);
	setbkmode(TRANSPARENT);
	settextcolor(RGB(255, 85,185));
	outtextxy(10, 10, text);
}
void drawhealth(int health)
{
	static TCHAR text[64];
	_stprintf_s(text, _T("现在玩家的血量：%d"), health);
	setbkmode(TRANSPARENT);
	settextcolor(RGB(255, 85, 185));
	outtextxy(10, 30, text);
}


int main()
{
	initgraph(1280, 720,EW_SHOWCONSOLE);
	ExMessage msg;
	playerleft = new Atlas(_T("D:/ConsoleApplication1/ConsoleApplication1/back/player_left_%d.png"), 6);
	playerright = new Atlas(_T("D:/ConsoleApplication1/ConsoleApplication1/back/player_right_%d.png"), 6);
	enemyleft = new Atlas(_T("D:/ConsoleApplication1/ConsoleApplication1/back/enemy_left_%d.png"), 6);
	enemyright = new Atlas(_T("D:/ConsoleApplication1/ConsoleApplication1/back/enemy_right_%d.png"), 6);
	Player player;
	IMAGE background;
	IMAGE memu;
	vector<Enemy*>enemys;
	
	RECT regionstart, regionfinish;
	regionstart.left = (1280 - buttonwidth) / 2;
	regionstart.right= (1280 + buttonwidth) / 2;
	regionstart.top = 430;
	regionstart.bottom = regionstart.top + buttonheight;
	regionfinish.left = (1280 - buttonwidth) / 2;
	regionfinish.right = (1280 + buttonwidth) / 2;
	regionfinish.top = 550;
	regionfinish. bottom= regionfinish.top + buttonheight;
	startbutton start_game = startbutton(regionstart, _T("D:/ConsoleApplication1/ConsoleApplication1/back/ui_start_idle.png"),
		_T("D:/ConsoleApplication1/ConsoleApplication1/back/ui_start_hovered.png"), _T("D:/ConsoleApplication1/ConsoleApplication1/back/ui_start_pushed.png"));
	finishbutton finish_game = finishbutton(regionfinish, _T("D:/ConsoleApplication1/ConsoleApplication1/back/ui_quit_idle.png"),
		_T("D:/ConsoleApplication1/ConsoleApplication1/back/ui_quit_hovered.png"), _T("D:/ConsoleApplication1/ConsoleApplication1/back/ui_quit_pushed.png"));
	loadimage(&memu, _T("D:/ConsoleApplication1/ConsoleApplication1/back/menu.png"));
	loadimage(&background, _T("D:/ConsoleApplication1/ConsoleApplication1/back/background.png"));
	vector<Bullet>bullets(2 + score);
	BeginBatchDraw();
	

	while (running)
	{
		
		/*vector<Bullet*>bullets1;
		vector<POINT>points;
		if (msg.message = WM_LBUTTONDOWN)
			{
				POINT point = {player.getplayer().x,player.getplayer().y};
				bullets1.push_back(new Bullet(point,5,30,10));
				point.x = msg.x;
				point.y = msg.y;
				
				points.push_back(point);
			}
			for (int i = 0; i < bullets1.size(); i++)
			{
				bullets1[i]->bulletfly(points[i]);
			}*/
		DWORD time = GetTickCount();
		while (peekmessage(&msg))
		{
			
			if(gamestart==1)
			player.move1(msg);
			else
			{
				start_game.possessevent(msg);
				finish_game.possessevent(msg);
			}
		}
		if (gamestart)
		{
			
			createnewenemy(enemys);
			player.move2();
			for (Enemy* enemy : enemys)
			{
				enemy->move(player);
			}
			bulletfollow(bullets, player);
			for (Enemy* enemy : enemys)
			{
				
				if (enemy->killplayer(player))
				{
					static TCHAR text[128];
					_stprintf_s(text, _T("目前玩家得分:%d"), score);
					MessageBox(GetHWnd(), text, _T("游戏结束"), MB_OK);
					running = 0;
					break;
				}
			}
			if (!enemys.empty())//如果enemys非空
			{
				for (auto it : enemys)//遍历
				{
					it->aidasuan();
					if (it->canaida())//判断
					{
						for (auto bul : bullets)//如果能被打，再遍历子弹
							if (it->judge(bul))//是否命中
							{
								it->health -= playerhurt;//命中后：扣血、受击cd重置
								it->zeroaida();
								it->hunt();
							}
					}
				}
			}

			for (size_t i = 0; i < enemys.size(); i++)
			{
				Enemy* enemy = enemys[i];
				if (!enemy->checklive())
				{
					swap(enemys[i], enemys.back());
					enemys.pop_back();
					delete enemy;
					score++;
				}
			}
		}
		
		cleardevice();
		
		if (gamestart)
		{
			putimage(0, 0, &background);
			drawscore(score);
			drawhealth(playerhealth);
			player.Drawplayer(1000 / 144);
			for (Enemy* enemy : enemys)
			{
				enemy->DrawEnemy(1000 / 144,player);
			}
			for (Bullet& bullet : bullets)
			{
				bullet.draw();
			}
			/*for (int i = 0; i<bullets1.size(); i++)
			{
				bullets1[i]->draw();
			}*/
		}
		else
		{
			putimage(0, 0, &memu);
			start_game.draw();
			finish_game.draw();
		}
		FlushBatchDraw();
		
		DWORD endtime = GetTickCount();
		DWORD time1 = endtime - time;
		if (time1 < 1000 / 144)
		{
			Sleep(1000 / 144 - time1);
		}
	}
	
	EndBatchDraw();
	delete playerleft;
	delete playerright;
	delete enemyleft;
	delete enemyright;
	return 0;
}